package com.kai.mqtt.message;

import com.kai.mqtt.util.LoadStrategyUtil;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author weijk
 * @since 2024/6/17
 */
public class ShareSubscribeMessage {

    private static final List<SubscribeMessage> LIST = new CopyOnWriteArrayList<>();

    /**
     * 当前索引
     */
    private Integer currentIndex;

    public void add(SubscribeMessage subscribeMessage) {
        LIST.add(subscribeMessage);
    }

    public void remove(String clientId) {
        LIST.removeIf(subscribeMessage -> Objects.equals(subscribeMessage.getClientId(), clientId));
    }

    public boolean isEmpty() {
        return LIST.isEmpty();
    }

    public SubscribeMessage get() {
        currentIndex = LoadStrategyUtil.getIndex(currentIndex, LIST.size());
        return LIST.get(currentIndex);
    }

    public Integer size() {
        return LIST.size();
    }

    public List<SubscribeMessage> list() {
        return LIST;
    }
}
